package sparrowGui.components.item
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	
	import sparrowGui.event.ItemEvent;
	
	/** 项数据更新. **/
	[Event(name="item_update", 	type="sparrowGui.event.ItemEvent")]
	/** 项选中状态改变. **/
	[Event(name="item_select_change", 	type="sparrowGui.event.ItemEvent")]
	/** 项可用状态改变. **/
	[Event(name="item_ebable_change", 	type="sparrowGui.event.ItemEvent")]
	
	/**
	 * 富项,除了基本的鼠标四态外，还添了选中和不可选状态
	 * 操作跟RichMCBtn一样，不过本控件是visible控制的
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SRichItem extends SItem
	{
		static protected const SELECTSTATE_NAME:String = "selectState";
		static protected const ENABLEDSTATE_NAME:String = "enabledState";
		
/*		public function get selectState():Sprite{ return skin["selectState"] as Sprite };
		public function get disableState():Sprite{ return skin["enabledState"] as Sprite };*/
		protected var selectState:DisplayObject;
		protected var enabledState:DisplayObject;
		
		/**
		 * 选中状态
		 */
		private var _selected:Boolean;
		private var _data:Object;
		
		public function SRichItem(uiVars:Object=null)
		{
			super(uiVars);
		}
		
		override public function update(o:Object):void
		{
			_data = o;
			super.update(o);
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_UPDATE));
		}
		
		override protected function setUI(uiVars:Object=null):void
		{
//			argSkin = argSkin || SkinStyleSheet.getIns().richItemSkin;
			super.setUI(uiVars);
			
			if(skin is DisplayObjectContainer)
			{
				var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
				skinDC.mouseChildren = false;
				
				selectState = skinDC.getChildByName(SELECTSTATE_NAME);
				enabledState = skinDC.getChildByName(ENABLEDSTATE_NAME);
			}
			
			_selected = false;
			super.enabled = true;
		}
		
/*		override protected function addSkinListen():void
		{
			super.addSkinListen();
			skin.addEventListener(MouseEvent.CLICK,onMouseEvt);
		}
		
		override protected function removeSkinListen():void
		{
			super.removeSkinListen();
			skin.removeEventListener(MouseEvent.CLICK,onMouseEvt);
		}
		
		override protected function onMouseEvt(e:Event):void
		{
			if(!enabled)
				return;
			
			// 点击反选
			if(e.type == MouseEvent.CLICK){
				selected = !selected;
				return;
			}
			
			// 选中的时候把其它的事件过滤
			if(selected)
				return;
			
			super.onMouseEvt(e);
		}*/
		
		///////////////////////////////////
		// get/set
		//////////////////////////////////
		
		
		override public function getDefaultUIName():String
		{
			return "richItem";
		}
		
		override protected function get states():Array
		{
			return [upState,overState,downState,selectState,enabledState];
		}
		
/*		override protected function hiddenState():void
		{
			var itemStates:Array = [upState,overState,downState,hitTestState,selectState,enabledState];
			for each(var dp:DisplayObject in itemStates)
			if(dp) dp.visible = false;
		}*/
		
		/**
		 * 激活选中事件
		 
		public function activeSelect():void
		{
			skin.addEventListener(MouseEvent.CLICK,onMouseEvt);
		}*/
		
		/**
		 * 反激活选中事件
		 
		public function unActiveSelect():void
		{
			skin.removeEventListener(MouseEvent.CLICK,onMouseEvt);
		}*/
		
		/**
		 * 选中状态
		 */
		override public function set selected(value:Boolean):void
		{
			_selected = value;
			if(_selected){
				changeState(SELECTSTATE_NAME);
			}else{
				changeState(UPSTATE_NAME);
			}
//			dispatchEvent(new ItemEvent(ItemEvent.ITEM_SELECT_CHANGE));
		}
		
		override public function get selected():Boolean
		{
			return _selected;
		}
		
		/**
		 * 能否使用
		 */
		override public function set enabled(value:Boolean):void
		{
			super.enabled = value;
			if(super.enabled){
				selected = _selected;
//				removeListener();
//				addListener();
			}else{
				changeState(ENABLEDSTATE_NAME);
//				removeListener();
			}
//			dispatchEvent(new ItemEvent(ItemEvent.ITEM_EBABLE_CHANGE));
			
			if(skin is InteractiveObject)
				(skin as InteractiveObject).mouseEnabled = value;
		}
		
		override public function get itemData():Object 
		{
			return _data;
		}
		
		override public function dispose():void
		{
			selected = false;
			enabled = true;
			_data = null;
			super.dispose();
		}
	}
}